import { prisma } from '../db'
import type { User } from '@prisma/client'

export class UserService {
  /**
   * 创建新用户
   */
  static async createUser(data: {
    email: string
    username: string
    password: string
  }): Promise<User> {
    return await prisma.user.create({
      data
    })
  }

  /**
   * 根据 ID 获取用户
   */
  static async getUserById(id: number): Promise<User | null> {
    return await prisma.user.findUnique({
      where: { id }
    })
  }

  /**
   * 根据邮箱获取用户
   */
  static async getUserByEmail(email: string): Promise<User | null> {
    return await prisma.user.findUnique({
      where: { email }
    })
  }

  /**
   * 根据用户名获取用户
   */
  static async getUserByUsername(username: string): Promise<User | null> {
    return await prisma.user.findUnique({
      where: { username }
    })
  }

  /**
   * 更新用户信息
   */
  static async updateUser(id: number, data: Partial<Omit<User, 'id' | 'createdAt' | 'updatedAt'>>): Promise<User> {
    return await prisma.user.update({
      where: { id },
      data
    })
  }

  /**
   * 删除用户
   */
  static async deleteUser(id: number): Promise<User> {
    return await prisma.user.delete({
      where: { id }
    })
  }

  /**
   * 获取所有用户
   */
  static async getAllUsers(): Promise<User[]> {
    return await prisma.user.findMany({
      orderBy: { createdAt: 'desc' }
    })
  }
}
